home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / packages / compare-w.el.z / compare-w.el
Encoding:
Text File  |  1998-05-21  |  6.1 KB  |  181 lines

  1. ;;; compare-w.el --- compare text between windows for Emacs.
  2.  
  3. ;; Copyright (C) 1986, 1989, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23.  
  24. ;;; Synched up with: FSF 19.34.
  25.  
  26. ;;; Whatever was here before didn't look any more correct than the
  27. ;;; FSF version, so I've junked it and replaced it with the FSF version.
  28. ;;; If you really don't like this, dig out the previous version from
  29. ;;; 19.13.  --ben
  30.  
  31. ;;; Commentary:
  32.  
  33. ;; This package provides one entry point, compare-windows.  It compares
  34. ;; text starting from point in two adjacent windows, advancing point
  35. ;; until it finds a difference.  Option variables permit you to ignore
  36. ;; whitespace differences, or case differences, or both.
  37.  
  38. ;;; Code:
  39.  
  40. (defvar compare-windows-whitespace "[ \t\n]+"
  41.   "*Regexp that defines whitespace sequences for \\[compare-windows].
  42. Changes in whitespace are optionally ignored.
  43.  
  44. The value of `compare-windows-whitespace' may instead be a function; this
  45. function is called in each buffer, with point at the current scanning point.
  46. The function's job is to categorize any whitespace around (including before)
  47. point; it should also advance past any whitespace.
  48.  
  49. The function is passed one argument, the point where `compare-windows'
  50. was originally called; it should not consider any text before that point.
  51. If the function returns the same value for both buffers, then the
  52. whitespace is considered to match, and is skipped.")
  53.  
  54. (defvar compare-ignore-case nil
  55.   "*Non-nil means \\[compare-windows] ignores case differences.")
  56.  
  57. ;;;###autoload
  58. (defun compare-windows (ignore-whitespace)
  59.   "Compare text in current window with text in next window.
  60. Compares the text starting at point in each window,
  61. moving over text in each one as far as they match.
  62.  
  63. This command pushes the mark in each window
  64. at the prior location of point in that window.
  65. If both windows display the same buffer,
  66. the mark is pushed twice in that buffer:
  67. first in the other window, then in the selected window.
  68.  
  69. A prefix arg means ignore changes in whitespace.
  70. The variable `compare-windows-whitespace' controls how whitespace is skipped.
  71. If `compare-ignore-case' is non-nil, changes in case are also ignored."
  72.   (interactive "P")
  73.   (let* (p1 p2 maxp1 maxp2 b1 b2 w2
  74.         success size
  75.         (opoint1 (point))
  76.         opoint2
  77.         (skip-whitespace (if ignore-whitespace
  78.                  compare-windows-whitespace)))
  79.     (setq p1 (point) b1 (current-buffer))
  80.     (setq w2 (next-window (selected-window)))
  81.     (if (eq w2 (selected-window))
  82.     (error "No other window"))
  83.     (setq p2 (window-point w2)
  84.       b2 (window-buffer w2))
  85.     (setq opoint2 p2)
  86.     (setq maxp1 (point-max))
  87.     (save-excursion
  88.       (set-buffer b2)
  89.       (push-mark p2 t)
  90.       (setq maxp2 (point-max)))
  91.     (push-mark)
  92.  
  93.     (setq success t)
  94.     (while success
  95.       (setq success nil)
  96.       ;; if interrupted, show how far we've gotten
  97.       (goto-char p1)
  98.       (set-window-point w2 p2)
  99.  
  100.       ;; If both buffers have whitespace next to point,
  101.       ;; optionally skip over it.
  102.  
  103.       (and skip-whitespace
  104.        (save-excursion
  105.          (let (p1a p2a w1 w2 result1 result2)
  106.            (setq result1
  107.              (if (stringp skip-whitespace)
  108.              (compare-windows-skip-whitespace opoint1)
  109.                (funcall skip-whitespace opoint1)))
  110.            (setq p1a (point))
  111.            (set-buffer b2)
  112.            (goto-char p2)
  113.            (setq result2
  114.              (if (stringp skip-whitespace)
  115.              (compare-windows-skip-whitespace opoint2)
  116.                (funcall skip-whitespace opoint2)))
  117.            (setq p2a (point))
  118.            (if (or (stringp skip-whitespace)
  119.                (and result1 result2 (eq result1 result2)))
  120.            (setq p1 p1a
  121.              p2 p2a)))))
  122.  
  123.       ;; Try advancing comparing 1000 chars at a time.
  124.       ;; When that fails, go 500 chars at a time, and so on.
  125.       (let ((size 1000)
  126.         success-1
  127.         (case-fold-search compare-ignore-case))
  128.     (while (> size 0)
  129.       (setq success-1 t)
  130.       ;; Try comparing SIZE chars at a time, repeatedly, till that fails.
  131.       (while success-1
  132.         (setq size (min size (- maxp1 p1) (- maxp2 p2)))
  133.         (setq success-1
  134.           (and (> size 0)
  135.                (= 0 (compare-buffer-substrings b2 p2 (+ size p2)
  136.                                b1 p1 (+ size p1)))))
  137.         (if success-1
  138.         (setq p1 (+ p1 size) p2 (+ p2 size)
  139.               success t)))
  140.       ;; If SIZE chars don't match, try fewer.
  141.       (setq size (/ size 2)))))
  142.  
  143.     (goto-char p1)
  144.     (set-window-point w2 p2)
  145.     (if (= (point) opoint1)
  146.     (ding))))
  147.  
  148. ;; Move forward over whatever might be called whitespace.
  149. ;; compare-windows-whitespace is a regexp that matches whitespace.
  150. ;; Match it at various starting points before the original point
  151. ;; and find the latest point at which a match ends.
  152. ;; Don't try starting points before START, though.
  153. ;; Value is non-nil if whitespace is found.
  154.  
  155. ;; If there is whitespace before point, but none after,
  156. ;; then return t, but don't advance point.
  157. (defun compare-windows-skip-whitespace (start)
  158.   (let ((end (point))
  159.     (beg (point))
  160.     (opoint (point)))
  161.     (while (or (and (looking-at compare-windows-whitespace)
  162.             (<= end (match-end 0))
  163.             ;; This match goes past END, so advance END.
  164.             (progn (setq end (match-end 0))
  165.                (> (point) start)))
  166.            (and (/= (point) start)
  167.             ;; Consider at least the char before point,
  168.             ;; unless it is also before START.
  169.             (= (point) opoint)))
  170.       ;; keep going back until whitespace
  171.       ;; doesn't extend to or past end
  172.       (forward-char -1))
  173.     (setq beg (point))
  174.     (goto-char end)
  175.     (or (/= beg opoint)
  176.     (/= end opoint))))
  177.  
  178. (provide 'compare-w)
  179.  
  180. ;;; compare-w.el ends here
  181.